﻿Imports System.ServiceModel
Imports System.ServiceModel.Description
Imports System.IO

' These namespaces are found in the Microsoft.Xrm.Sdk.dll assembly
' located in the SDK\bin folder of the SDK download.
Imports Microsoft.Xrm.Sdk
Imports Microsoft.Xrm.Sdk.Query
Imports Microsoft.Xrm.Sdk.Client
Imports Microsoft.Crm.Sdk.Messages
Imports Microsoft.Xrm.Sdk.Messages
Imports Microsoft.Xrm.Sdk.Metadata
Imports Microsoft.Xrm.Sdk.Discovery
Imports System.Collections.Generic
Imports Microsoft.Crm.Sdk.Samples
Imports System.Data.SqlClient
Imports System.Linq
Module Module1

    Public Class LoggerClass
        Public Shared Sub Run()
            Dim crmcon As New ServerConnection
            Dim serverConfig As New ServerConnection.Configuration
            serverConfig = crmcon.GetServerConfiguration()

            Try

                ' Connect to the Organization service. 
                'The using statement assures that the service proxy will be properly disposed.
                Using _serviceProxy = New OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri, serverConfig.Credentials, serverConfig.DeviceCredentials)
                    ' This statement is required to enable early-bound type support.
                    _serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(New ProxyTypesBehavior())

                    Dim service As IOrganizationService = DirectCast(_serviceProxy, IOrganizationService)


                    Using output As New StreamWriter("output.txt")



                        Dim slos As New SoapLoggerOrganizationService(serverConfig.OrganizationUri, service, output)

                        '***********************************************************************
                        '***********************************************************************
                        '************ START OF CODE TO FOR LOGGED REQUEST ***********************
                        '***********************************************************************
                        '***********************************************************************


                        Dim reqWAI As New WhoAmIRequest()
                        Dim respWAI As WhoAmIResponse = DirectCast(_serviceProxy.Execute(reqWAI), WhoAmIResponse)

                        Dim reqSchedule As New QueryScheduleRequest()
                        reqSchedule.ResourceId = respWAI.UserId
                        reqSchedule.Start = DateTime.Now
                        reqSchedule.End = DateTime.Today.AddDays(3)
                        Dim tmc As New TimeCode()
                        tmc = TimeCode.Available
                        reqSchedule.TimeCodes = New TimeCode() {TimeCode.Available}

                        Dim respSchedule As QueryScheduleResponse = DirectCast(slos.Execute(reqSchedule), QueryScheduleResponse)

                        If respSchedule.TimeInfos.Length > 0 Then
                            'act on timeslots
                        Else
                            'user has no free time that meets these parameters
                        End If





      





                            '***********************************************************************
                            '***********************************************************************
                            '************ END OF CODE FOR LOGGED REQUEST ***********************
                            '***********************************************************************
                            '***********************************************************************

                    End Using




                End Using

                ' Catch any service fault exceptions that Microsoft Dynamics CRM throws.
            Catch generatedExceptionName As FaultException
                ' You can handle an exception here or pass it back to the calling method.
                Throw
            End Try
        End Sub
    End Class

    Sub Main()
        Try

            LoggerClass.Run()

        Catch ex As FaultException
            Console.WriteLine("The application terminated with an error.")
            Console.WriteLine(ex.ToString)
            Console.WriteLine("Code: {0}", ex.Code)
            Console.WriteLine("Message: {0}", ex.Message)
            'Console.WriteLine("Trace: {0}", ex.Detail.TraceText);
            'Console.WriteLine("Inner Fault: {0}", If(ex.Detail.InnerFault Is Nothing, "Has Inner Fault", "No Inner Fault"))
        Catch ex As System.TimeoutException
            Console.WriteLine("The application terminated with an error.")
            Console.WriteLine("Message: {0}", ex.Message)
            Console.WriteLine("Stack Trace: {0}", ex.StackTrace)
            'Console.WriteLine("Inner Fault: {0}", If(ex.InnerException.Message Is Nothing, "Has Inner Fault", "No Inner Fault"))
        Catch ex As System.Exception
            Console.WriteLine("The application terminated with an error.")
            Console.WriteLine(ex.Message)

            ' Display the details of the inner exception.
            If ex.InnerException IsNot Nothing Then
                Console.WriteLine(ex.InnerException.Message)

                Dim fe As FaultException(Of Microsoft.Xrm.Sdk.OrganizationServiceFault) = TryCast(ex.InnerException, FaultException(Of Microsoft.Xrm.Sdk.OrganizationServiceFault))
                If fe IsNot Nothing Then
                    Console.WriteLine("Timestamp: {0}", fe.Detail.Timestamp)
                    Console.WriteLine("Code: {0}", fe.Detail.ErrorCode)
                    Console.WriteLine("Message: {0}", fe.Detail.Message)
                    Console.WriteLine("Trace: {0}", fe.Detail.TraceText)
                    'Console.WriteLine("Inner Fault: {0}", If(fe.Detail.InnerFault Is Nothing, "Has Inner Fault", "No Inner Fault"))
                End If
            End If
        Finally

            ' Additional exceptions to catch: SecurityTokenValidationException, ExpiredSecurityTokenException,
            ' SecurityAccessDeniedException, MessageSecurityException, and SecurityNegotiationException.

            Console.WriteLine("Press <Enter> to exit.")
            Console.ReadLine()
        End Try
    End Sub



End Module


